package lesson26;

public class MyCAS1 {

    private volatile int value;

    public synchronized int compareAndSwap(int expectValue, int newValue){

        int oldValue = value;

        if(value == expectValue){
            value = newValue;
            System.out.println(Thread.currentThread().getName() + "修改成功...");
        }else{
            System.out.println(Thread.currentThread().getName() + "修改失败...");
        }

        return oldValue;
    }

    public static void main(String[] args) {

        MyCAS1 myCAS1 = new MyCAS1();

        myCAS1.value = 100;

        for(int i = 0; i < 100; i++){
            new Thread(() -> {
                myCAS1.compareAndSwap(100, 150);
            }).start();
        }

    }
}
